<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript">
$(function() {
	
	var urlPrefix = "${pageContext.request.contextPath}/api/";
	var tenderData = new Array();
	var tabs = $.getTabStack();
	
	$('#bidding_list_search_title').searchbox({
		height: 28,
		width: 300,
		searcher: function(value, name) {
		    $('#bidding_list_grid').datagrid({
        	    queryParams: {
                    param: JSON.stringify({
                        tender_title: value
                    })
                }
        	});
		}
	});
	
	$("#bidding_list_grid").datagrid({
	    url: '${pageContext.request.contextPath}/api/TenderSearchAPI.json',
	    columns: [[
            {field:'tender_title', title:'招标名称', width:240, align:'left', halign:'center', resizable:false,
                formatter: function(value, row, index) {
                    return '<a href="javascript:void(0);" class="bidding_list_tender_title">' + row.tender_title + '</a>';
                }
            },
            {field:'enroll_start_date', title:'报名开始日', width:100, align:'center', halign:'center', resizable:false},
            {field:'enroll_end_date', title:'报名截止日', width:100, align:'center', halign:'center', resizable:false},
            {field:'tender_end_date', title:'投标截止日', width:100, align:'center', halign:'center', resizable:false},
            {field:'publish_result_date', title:'中标公布日', width:100, align:'center', halign:'center', resizable:false},
            {field:'enroll_count', title:'已报名', width:60, align:'right', halign:'center', resizable:false},
            {field:'finalist_count', title:'已入围', width:60, align:'right', halign:'center', resizable:false},
            {field:'quote_count', title:'已投标', width:60, align:'right', halign:'center', resizable:false},
            {field:'bid_count', title:'已中标', width:60, align:'right', halign:'center', resizable:false},
            {field:'status_name', title:'状态', width:80, align:'center', halign:'center', resizable:false},
            {field:'opt', title:'操作', width:80, align:'center', halign:'center', resizable:false,
                formatter: function(value, row, index) {
                    return '<a href="javascript:void(0);" class="bidding_list_button_manage">管理</a>';
                }
            }
        ]],
		striped: true,
		rownumbers: true,
		singleSelect: true,
        autoRowHeight: false,
		idField : 'tender_id',
		toolbar: '#bidding_list_toolbar',
		pagination : true,
		pageSize: 10,
        pageList: [10],
		onSelect : function (rowIndex, rowData){
			$("#bidding_list_grid").datagrid('unselectRow',rowIndex);
		},
        loadFilter: function(data) {
        	var datax = {};
        	if (data.total && data.total > 0) {
    			datax.total = data.total;
    			datax.rows = data.tenders;
        	} else {
        		datax.total = 0;
    			datax.rows = new Array();
        	}
        	return datax;
        },
        onLoadSuccess: function(data) {
        	if (data.total > 0) {
        	    tenderData = data.rows;
        	    
        	    $('.bidding_list_button_manage').linkbutton({
                    iconCls: 'icon-edit',
                    onClick: function() {
                        var index = $(this).index('.bidding_list_button_manage');
                        var tenderId = tenderData[index].tender_id;
                        var tenderTitle = tenderData[index].tender_title;
                        var tenderStatus = tenderData[index].status;
                        if (tabs.indexOf("tab_tender_bidding_list_sub") > 0) {
                			$('#main_tabs').tabs("close", tabs.indexOf("tab_tender_bidding_list_sub"));
                		}
                        var tabTitle = '应标管理-';
                        if (tenderTitle.length > 10) {
                            tabTitle = tabTitle + tenderTitle.substr(0, 10) + '...';
                        } else {
                            tabTitle = tabTitle + tenderTitle;
                        }
                        $('#main_tabs').tabs('add', {
            				id : 'tab_tender_bidding_list_sub',
            				title : tabTitle,
            				selected : false,
            				closable : true,
            				style : {
            					padding : 10
            				},
            				href : 'tender_manage.action?tenderId=' + tenderId
            			});
            			tabs.push("tab_tender_bidding_list_sub");
                        $('#main_tabs').tabs("select", tabs.indexOf("tab_tender_bidding_list_sub"));
                    }
                });
        	    
        	    $('.bidding_list_tender_title').click(function() {
        	        var index = $(this).index('.bidding_list_tender_title');
        	        var tenderId = tenderData[index].tender_id;
        	        $.showTenderDetailWindow(tenderId);
        	    });
        	}
        }
    });	

    $('#btn_bid_search').click(function() {
    	var tenderListAPI = new TenderListAPI({
            urlPrefix : urlPrefix
        });
    	var paramData = {};
    	paramData.tender_title = $('#search').textbox('getValue');
    	tenderListAPI.search({
  	        onSuccess : function(data) {
  	        	$('#bidding_list_grid').datagrid("loadData", data);
	        },
	        onAPIError : function(status, errors) {
	        },
	        onServerError : function(jqXHR, textStatus, errorThrown) {
	        }
	    },
	    {
	        param: JSON.stringify(paramData)
	    });
	});
    
});
</script>
<table id="bidding_list_grid"></table>
<div id="bidding_list_toolbar">
  <p>招标名称：<input type="text" id="bidding_list_search_title"></p>
</div>
<div id="tender_manage_win"></div>
<div id="information_window"></div>
